package com.lgkj.ui;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.LruCache;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.lgkj.R;

import java.util.List;

/**
 * Created by Administrator on 2018/7/6 0006.
 */

public class PictureDisplayActivity extends Activity {
    private ImageView imageView;
    private TextView pictureDisplayText;
    int downX = 0;
    int moveX = 0;
    int upX = 0;
    int a = 1; //显示的图片位置
    int imageKey = 1;

    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 9;//设置缓冲空间存储大小
    private LruCache<String, Bitmap> mMemoryCache = new LruCache<>(cacheSize);
    Bitmap bitmap = null;

    private final String TAG = "PictureDisplayActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_display);

        initView();
    }

    private void initView() {

        imageView =findViewById(R.id.picture_display_image);

    }

    /**
     * 显示下载图片
     */
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                updateImageView((Bitmap) msg.obj);
            }
        }
    };

    /**
     * //将bitmap添加到缓存中
     *
     * @param bm
     */
    private void updateImageView(Bitmap bm) {
        //list.add(bm);

        mMemoryCache.put("imageKey" + imageKey, bm);
        imageView.setImageBitmap(mMemoryCache.get("imageKey" + a));
        // Log.e("NSC", "=====" + bm.toString());
        imageKey++;
    }

    /**
     *
     */
    private Runnable runa = new Runnable() {
        @Override
        public void run() {

            Intent intent = getIntent();
            //int position = intent.getIntExtra("position");
            int position = intent.getIntExtra("position", -1);
            List url = intent.getStringArrayListExtra("enlargeImage");
            Log.e(TAG, "a==" + position);
            a = position + 1;
            pictureDisplayText.setText((Integer.valueOf(a)) + "/" + Integer.valueOf(url.size()));
            //  bit(urlPosition);//先显示传进来的第一张
            for (int i = 0; i < url.size(); i++) {
                bit(url.get(i).toString());
                //Log.e("NSC",url.get(i).toString()+"i = "+i);
                // addView();
            }
        }
    };

    /**
     * @param url
     */
    private void bit(String url) {
//        byte[] data = new byte[0];
//        try {
//            data = ImageService.getImage(url);
//            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);  //生成位图
//            //  Looper.prepare();// 必须调用此方法，要不然会报错
//            Message msg = new Message();
//            msg.what = 0;
//            msg.obj = bitmap;
//            handler.sendMessage(msg);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://获取点击的时候屏幕位置
                downX = (int) event.getRawX();
                // Log.e("NSC","x = "+x+"  y="+y);
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = (int) event.getRawX();
                //Log.e("NSC","mx = "+mx+"  my="+my);
                break;
            case MotionEvent.ACTION_UP://手离开的时候位置
                upX = (int) event.getRawX();
                int pValue = upX - downX;//滑动的距离差
                Log.e("NSC", "upX-downX = " + (upX - downX));
                if (upX - downX > 120) {//右滑的时候
                    Log.e("NSC", "a = " + a + ":" + mMemoryCache.size());
                    if (a <= mMemoryCache.size()) {
                        // Log.e(TAG, mMemoryCache.get("imageKey"+a).getWidth()+":"+mMemoryCache.get("imageKey"+a).getHeight());
                        //mMemoryCache.get("imageKey"+a).getWidth();
                        imageView.setImageBitmap(mMemoryCache.get("imageKey" + a));
                        pictureDisplayText.setText((Integer.valueOf(a)) + "/" + Integer.valueOf(mMemoryCache.size()));
                        a++;
                        if (a > mMemoryCache.size()) {
                            a = 1;
                        }
                    }

                } else if (upX - downX < -120) {//左滑
                    Log.e("NSC", "aaaa = " + a + ":" + mMemoryCache.size());
                    if (a > 0) {
                        imageView.setImageBitmap(mMemoryCache.get("imageKey" + a));
                        pictureDisplayText.setText((Integer.valueOf(a)) + "/" + Integer.valueOf(mMemoryCache.size()));
                        a--;
                        if (a == 0) {
                            a = mMemoryCache.size();
                        }
                    }

                }

                if (pValue == 0) {//点击触摸就关闭窗口
                    finish();
                }

                break;
        }

        return super.onTouchEvent(event);         // 注册手势事件
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null && !bitmap.isRecycled()) {
            // 回收并且置为null
            bitmap.recycle();
            bitmap = null;
        }
    }
}

